import {request} from '../../utils/http'


Page({
  data: {
    orderStatus:'',
    info:{},
    smid:''
  },
  getOrderInfo(id){
    request(`/order/show_ecrypt`, 'get', {
      order_id:id
    }).then(res => {
      console.log(res, 'order info')
      if (res.code == 200) {
        let info = res.data;
        this.setData({
          info
        });
        if (info.status == 10) {
          this.initPayData(info)
        }

        if (info.status == 20) {
          this.initSearchData(info)

        }

      }
    })

  },
  initSearchData(order){
    const plugin = requirePlugin('dynamic-plugin://2021003126645865');
    const ext = my.getExtConfigSync()

    plugin.init({
      queryOptions: {
         merchantPid: this.data.smid, // 必传 - 商户PID
         productNo: order.product_no, // 必传 - 产品编号
         subscriptionNo: order.alipay_order_no, //必传 - 订购编号
      }
    });
    my.navigateTo({
      url: 'dynamic-plugin://2021003126645865/index'
    })
  },
  initPayData(order) {
    // 引用插件实例
    const plugin = requirePlugin('dynamic-plugin://2021003126645865');
    const ext = my.getExtConfigSync()
    console.log(ext.pid)
    
    // 宿主向插件传参数，通过实例调用init方法传参
    console.log(this.data.product, 'product');
    let d = new Date();
    // 开通
    const options = {
      merchantPid: this.data.smid, // 必传 - 商户PID
      productNo: order.product_no, // 必传 - 产品编号
      
      subscriptionType: order.pay_date == 1 ? 'MONTH' : 'WEEK', // 非必填, 订购类型, 默认为月卡
      productName: order.product_title, //必传 - 产品名称
      deductionAmount: order.second_amount, // 必传 - 每次扣减固定金额
      periods: order.total_stage, // 必传 - 开通总周期数
      downPayment: order.first_amount, // 非必填 - 前N期金额
      downPaymentPeriods: order.first_stage, // 非必填 -前多少期价格不一样，不传默认为1
      downPaymentDate: order.first_stage_date,
      outSubscriptionNo: order.out_order_no
    }
    console.log('options', options)
    plugin.init({
      openOptions: options
    });

    my.navigateTo({
      url: 'dynamic-plugin://2021003126645865/index'
    })
  
    //plugin.setEnv('dev');
    
    // 查询
  
  },
  onShow(){
    console.log(this.data.orderStatus, 'show')
  },
  goBack(){
    let url = this.data.info.redirect_url;
    console.log(url)
    let params = this.schemeToParams(url)
    if (params) {
      my.navigateToMiniProgram({
        ...params,
        fail: (res) => my.alert({ title: '跳转小程序失败', content: JSON.stringify(res) }),
      });
    }
  },
  copy(){
    my.setClipboard({
      text:this.data.info.redirect_url,
      success(){
        my.showToast({content: '复制成功'});

      }
    })
  },
  schemeToParams(scheme) {
    if (!scheme.startsWith('alipays:')) {
      console.log('! 非 alipays: 开头');
      return null;
    }
    var query;
    var params = {};
    if (scheme.indexOf('?') > -1) {
      query = scheme.split('?')[1];
      let array = query.split('&')
      for (let k in array ) {
        let args = array[k].split('=');
        if (args[0] == 'appId') {
          params['appId'] = args[1];
        } else if (args[0] == 'page') {
          params['path'] = args[1];

        } else if (args[0] == 'query') {
          params['query'] = {};

        }
      }
    }
    return params;
  },
  onLoad(options) {
    console.log(options)
    this.setData({
      orderStatus:options.status
    });
    this.getOrderInfo(options.order_id)

    // if (options.status == 10 || options.status == 20) {
    //   my.loadPlugin({
    //     plugin: '2021003126645865@*', // 指定要加载的插件id和版本号，为 * 时自动选择版本
    //     success: () => {
    //       // 初始化数据 - 宿主传参
    //       console.log('success')

    //       this.getOrderInfo(options.order_id)
         
    //     },
    //     fail: (err) => {
    //       console.log(err)
    //     }
    //   });
    // } else {

    // }
    // request('/shop/smid','get', {shop_id:options.shop_id}).then(res => {
    //   if (res.code == 200) {
    //     this.setData({
    //       smid:res.data.smid
    //     }, () => {
          
    //     })
    //   }
    // });
    
  },
});
